package cn.matezk.iot;

import cn.hutool.core.util.ObjectUtil;
import org.apache.log4j.Logger;

public class Printer {
    private static Logger logger = Logger.getLogger(Printer.class);

    public static boolean printQr(String string, String label, int x, int y, int cellWidth) {
        // 0 = idle, 1 = head open, 16 = pause, following <ESC>!? command of TSPL manual
        byte status = TscLibDll.INSTANCE.usbportqueryprinter();
        if (status == -1) {
            logger.error("未发现打印机");
            return false;
        }
        TscLibDll.INSTANCE.openport("TSC DC2700");
        TscLibDll.INSTANCE.sendcommand("SIZE 20 mm, 15 mm");
        TscLibDll.INSTANCE.sendcommand("SPEED 4");
        TscLibDll.INSTANCE.sendcommand("DENSITY 12");
        TscLibDll.INSTANCE.sendcommand("DIRECTION 1");
        TscLibDll.INSTANCE.sendcommand("SET TEAR ON");
        TscLibDll.INSTANCE.clearbuffer();
        String content = String.format("B%04d%s", string.length(), string.replace("\"", "\\[\"]"));
        String cmd = String.format("QRCODE %d,%d,%s,%d,%s,%d,%s,%s,\"%s\"", x, y, TscLibDll.EccLevel.Q_PERCENT_25, cellWidth, TscLibDll.Mode.Manual, TscLibDll.Rotation.ANGLE_0, TscLibDll.Model.EXT, TscLibDll.Mask.S7, content);
        TscLibDll.INSTANCE.sendcommand(cmd);
        if (ObjectUtil.isNotEmpty(label)) {
            cmd = String.format("TEXT %d,%d,\"%d\",0,1,1,0,\"%s\"", 50, 85, 3, label);
            TscLibDll.INSTANCE.sendcommand(cmd);
        }
        TscLibDll.INSTANCE.printlabel("1", "1");
        TscLibDll.INSTANCE.clearbuffer();
        TscLibDll.INSTANCE.closeport();
        return true;
    }

    public static boolean printQr(String text, String label) {
        byte status = TscLibDll.INSTANCE.usbportqueryprinter();
        if (status == -1) {
            logger.error("未发现打印机");
            return false;
        }
        TscLibDll.INSTANCE.openport("TSC DC2700");
        TscLibDll.INSTANCE.sendcommand("SIZE 20 mm, 15 mm");
        TscLibDll.INSTANCE.sendcommand("SPEED 4");
        TscLibDll.INSTANCE.sendcommand("DENSITY 12");
        TscLibDll.INSTANCE.sendcommand("DIRECTION 1");
        TscLibDll.INSTANCE.sendcommand("SET TEAR ON");
        TscLibDll.INSTANCE.clearbuffer();
        String content = String.format("B%04d%s", text.length(), text.replace("\"", "\\[\"]"));
        String cmd = String.format("QRCODE %d,%d,%s,%d,%s,%d,%s,%s,\"%s\"", 48, 5, TscLibDll.EccLevel.Q_PERCENT_25, TscLibDll.CellWidth.W2, TscLibDll.Mode.Manual, TscLibDll.Rotation.ANGLE_0, TscLibDll.Model.EXT, TscLibDll.Mask.S7, content);
        TscLibDll.INSTANCE.sendcommand(cmd);
        cmd = String.format("TEXT %d,%d,\"%d\",0,1,1,0,\"%s\"", 55, 83, 3, label);
        TscLibDll.INSTANCE.sendcommand(cmd);
        TscLibDll.INSTANCE.printlabel("1", "1");
        TscLibDll.INSTANCE.clearbuffer();
        TscLibDll.INSTANCE.closeport();
        return true;
    }

//    public static void main(String[] args) {
//        printQr(JSONUtil.createObj().set("ProductId", "8F214HXEAM").set("DeviceName", "863505075163667").toString(), "3667");
//    }
}
